package com.lanbu.system.service.impl;

import com.lanbu.system.domain.mongo.MongoChatitems;
import com.lanbu.system.domain.mongo.MongoChats;
import com.lanbu.system.mapper.MongoChatsRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MongoChatitemsService {

    @Autowired
    private MongoChatsRepository mongoChatsRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<MongoChats> findAll() {
        return mongoChatsRepository.findAll();
    }

    public void updateAppIdByTeamId(ObjectId teamId, ObjectId newAppId) {
        // 创建查询条件
        Query query = new Query(Criteria.where("teamId").is(teamId));

        // 创建更新操作
        Update update = new Update().set("appId", newAppId);

        // 执行更新操作
        mongoTemplate.updateMulti(query, update, MongoChatitems.class);
    }

    public  List<MongoChats> findByappId(ObjectId id) {
        return mongoChatsRepository.findByappId(id);
    }
    public   List<MongoChats> findByTeamId(ObjectId teamId) {
        return mongoChatsRepository.findByTeamId(teamId);
    }


    public MongoChats save(MongoChats app) {
        return mongoChatsRepository.save(app);
    }
    public List<MongoChats> batchInsert(List<MongoChats> app) {
        List<MongoChats> mongoChats = mongoChatsRepository.saveAll(app);
        return mongoChats;
    }

    public void deleteById(String id) {
        mongoChatsRepository.deleteById(id);
    }

    // 你可以根据需求添加更多的方法
}
